import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";

import viteSentry from "vite-plugin-sentry";
import { ViteAliases } from "vite-aliases";
import versionPlugin from "./vite-plugin-version";
import viteImagemin from "vite-plugin-imagemin";
import { writeFileSync } from "fs";

const sentryConfig = {
  configFile: "./sentryclirc",
  deploy: {
    env: "production",
  },
  sourceMaps: {
    include: ["./dist/assets"],
    ignore: ["node_modules"],
    urlPrefix: "~/assets",
  },
};

// https://vite.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    {
      // 自定义插件确保文件位置正确
      name: "version-plugin",
      closeBundle() {
        const version = Date.now();
        writeFileSync("dist/version.json", JSON.stringify({ version }));
      },
    },
    viteSentry(sentryConfig),
    ViteAliases({
      prefix: "@", // 别名前缀
      deep: true, // 深度扫描目录
      depth: 2, // 扫描深度
      createLog: false, // 生成日志
    }),
    viteImagemin({
      gifsicle: { optimizationLevel: 7 }, // GIF 压缩配置
      mozjpeg: { quality: 50 }, // JPEG 质量
      pngquant: { quality: [0.8, 0.9] }, // PNG 质量范围
      svgo: {
        // SVG 优化
        plugins: [{ name: "removeViewBox" }],
      },
    }),
  ],

  build: {
    // souce-map文件可以让已编译过的代码映射出原始源, 信息文件, 里面储存着位置信息, 即转换后的代码的每一个位置所对应的转换前的位置
    sourcemap: true,
  },

  server: {
    host: "localhost",
    // host: "192.168.31.236",
    // https: {
    //   cert: fs.readFileSync(path.join(__dirname, "src/ssl/cert.crt")),
    //   key: fs.readFileSync(path.join(__dirname, "src/ssl/cert.key")),
    // },
    proxy: {
      "/api": {
        // target: "https://www.sushu.online/",
        target: "http://localhost:4000",
      },
    },
  },

  css: {
    modules: {
      localsConvention: "camelCaseOnly",
    },
    preprocessorOptions: {
      less: {
        javascriptEnabled: true,
        additionalData:
          '@import "src/assets/css/variable.less";@import "src/assets/css/theme.less";@import "src/assets/css/common.less";@import "src/assets/css/reset.less";',
      },
    },
  },
});
